package com.atguigu.day05;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

// 数据流中一共有三个事件
// Watermark(-MAX)
// (a, 1000)
// Watermark(MAX)
public class Example6 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .fromElements(Tuple2.of("a", 1000L))
                .keyBy(r -> r.f0)
                .process(new KeyedProcessFunction<String, Tuple2<String, Long>, String>() {
                    @Override
                    public void processElement(Tuple2<String, Long> in, Context ctx, Collector<String> out) throws Exception {
                        ctx.timerService().registerEventTimeTimer(in.f1 + 9999L);
                        out.collect("输入数据：" + in + "，当前process算子的并行子任务的水位线是：" +
                                "" + ctx.timerService().currentWatermark() + "，注册定时器：" +
                                "" + (in.f1 + 9999L));
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                        out.collect("定时器：" + timestamp + "触发了！，当前process算子的并行子任务的水位线是：" +
                                "" + ctx.timerService().currentWatermark());
                    }
                })
                .print();

        env.execute();
    }
}
